package com.example.administrator.xxnapp.fragment.picture;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.PictureBean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * Created by Administrator on 2018/4/23.
 */

public class RVPictureAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<PictureBean> mDatas;

    public RVPictureAdapter(Context context, List<PictureBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RVPictureViewHolder(LayoutInflater.from(context).inflate(R.layout.item_picture_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RVPictureViewHolder){
            ((RVPictureViewHolder) holder).bindView(position);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas == null?0:mDatas.size();
    }

    private class RVPictureViewHolder extends RecyclerView.ViewHolder{
        private ImageView mIV;
        private CardView mCardView;
        private SimpleDraweeView mDraweeView;

        public RVPictureViewHolder(View itemView) {
            super(itemView);
            mIV = itemView.findViewById(R.id.item_picture_IV);
            mCardView = itemView.findViewById(R.id.item_picture_CardView);
            mDraweeView = itemView.findViewById(R.id.item_picture_DraweeView);
        }

        private void bindView(final int position){
            RequestOptions options = new RequestOptions();
            options.centerCrop();
//            Glide.with(context).load(mDatas.get(position).getPath())
          /*  Glide.with(mIV.getContext())
                    .load("http://123.206.78.44"+mDatas.get(position).getPath())
                    .apply(options)
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .into(mIV);*/

            mDraweeView.setImageURI(Uri.parse("http://123.206.78.44"+mDatas.get(position).getPath()));
            /*.into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                    mIV.setImageDrawable(resource);
                }
            });*/
//            mIV.setTag(R.id.image_key);

            mIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onPictureItemClickListener != null){
                        onPictureItemClickListener.onPictureItemClick(position,mIV);
                    }
                }
            });
        }
    }

    private OnPictureItemClickListener onPictureItemClickListener;

    public void setOnPictureItemClickListener(OnPictureItemClickListener onPictureItemClickListener) {
        this.onPictureItemClickListener = onPictureItemClickListener;
    }

    public interface OnPictureItemClickListener{
        void onPictureItemClick(int position,View view);
    }
}
